package com.ruoyi.web.controller.wechatApplet;

import com.ruoyi.background.domain.AppSite;
import com.ruoyi.background.domain.AppWechatStep;
import com.ruoyi.background.domain.WechatPhone;
import com.ruoyi.background.domain.WechatStep;
import com.ruoyi.background.service.IAppSiteService;
import com.ruoyi.background.service.IAppWechatStepService;
import com.ruoyi.background.service.IWechatApiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信步数相关的接口
 */
@RestController
@RequestMapping("/wechat/step")
public class WechatStepController extends BaseController {

    @Autowired
    private IWechatApiService wechatApiService;

    @Autowired
    private IAppWechatStepService wechatStepService;

    @Autowired
    private IAppSiteService appSiteService;


    /**
     * 同步微信的步数
     *
     * @return 结果
     */
    @PostMapping("/syncStep")
    public AjaxResult getUserWechatStep(@RequestBody WechatPhone step) {
        AppWechatStep userStep = wechatApiService.getUserStepList(step);
        return AjaxResult.success(userStep);
    }

    /**
     * 获取用户累计的记录
     *
     * @return 结果
     */
    @GetMapping("/getUserCountData")
    public AjaxResult getUserCountData() {
        Integer userCountRow = wechatStepService.getUserCountRow();
        Integer userSumStep = wechatStepService.getUserSumStep();
        Integer userSumExchangeStep = wechatStepService.getUserSumExchangeStep();
        Map<String, Integer> res = new HashMap<>();
        res.put("daySum", userCountRow);
        res.put("stepSum", userSumStep);
        res.put("exchangeSum", userSumExchangeStep);
        return AjaxResult.success(res);
    }

    /**
     * 获取用户前后关卡
     *
     * @return 结果
     */
    @GetMapping("getNextAndBeforeSite")
    public AjaxResult getNextAndBeforeSite() {
        AppSite nextSite = appSiteService.getUserNextSite();
        AppSite beforeSite = appSiteService.getUserBeforeSite();
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("nextSite", nextSite);
        ajaxResult.put("beforeSite", beforeSite);
        return ajaxResult;
    }


    /**
     * 用户步数兑换积分
     *
     * @return 兑换的积分
     */
    @GetMapping("/exchangeStep")
    public AjaxResult userExchangeStep() {
        Integer grade = wechatStepService.userExchangeStep();
        return AjaxResult.success(grade);
    }

    /**
     * 获取兑换历史记录
     *
     * @param wechatStep 结果
     * @return 结果
     */
    @GetMapping("/getHistory")
    public TableDataInfo getUserHistory(AppWechatStep wechatStep) {
        startPage();
        wechatStep.setUserId(SecurityUtils.getUserId());
        List<AppWechatStep> list = wechatStepService.selectAppWechatStepList(wechatStep);
        return getDataTable(list);
    }


    /**
     * 获取微信历史步数
     *
     * @return 结果
     */
    @GetMapping("/getWechatHistory")
    public AjaxResult getUserWechatHistory() {
        List<WechatStep> userWechatHistory = wechatApiService.getUserWechatHistory();
        return AjaxResult.success(userWechatHistory);
    }


}
